package qc.module.platform.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.platform.dto.menu.*;
import qc.module.platform.entity.QcMenus;

import java.util.List;

/**
 * MenusMapper
 *
 * @author QuCheng Tech
 * @create 2023/02/07
 */
@Mapper
public interface MenusMapper {
    MenusMapper MAPPER = Mappers.getMapper(MenusMapper.class);

    /**
     * entity转为dto
     *
     * @param en
     * @return
     */
    @Mapping(target = "pcode", source = "projectcode")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "level", ignore = true)
    MenuDto toDto(QcMenus en);

    /**
     * dto转为entity
     *
     * @param dto
     * @return
     */
    @Mapping(target = "projectcode", source = "pcode")
    @Mapping(target = "odr", ignore = true)
    @Mapping(target = "flag", ignore = true)
    QcMenus toEntity(MenuDto dto);

    /**
     * entityList转为dtoList
     *
     * @param list
     * @return
     */
    List<MenuDto> toDtoList(List<QcMenus> list);

    @Mapping(target = "id", source = "id")
    @Mapping(target = "level", source = "level")
    @Mapping(target = "parentid", source = "parentid")
    @Mapping(target = "name", source = "title")
    @Named("toSimpleDto")
    MenuSimpleDto toSimpleDto(MenuDto en);

    /**
     * MenuDtoList转为MenuSimpleDtoList
     *
     * @param list
     * @return
     */
    @IterableMapping(qualifiedByName = "toSimpleDto")
    List<MenuSimpleDto> toSimpleDtoList(List<MenuDto> list);

    List<MenuTreeSimpleDto> toTreeSimpleDtoList(List<MenuTreeDto> list);

    @Mapping(target = "pcode", source = "projectcode")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "value", source = "id")
    @Mapping(target = "label", source = "title")
    @Mapping(target = "children", ignore = true)
    @Named("toTreeTableDetailDto")
    MenuTreeTableDetailDto toTreeTableDetailDto(QcMenus en);

    @IterableMapping(qualifiedByName = "toTreeTableDetailDto")
    List<MenuTreeTableDetailDto> toTreeTableDetailDtoList(List<QcMenus> list);

    /**
     * 新增修改DTO转换为Entity，特殊处理
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcMenus MenuDtoToEntity(MenuDto dto) {
        //DTO转换为Entity
        QcMenus en = MenusMapper.MAPPER.toEntity(dto);


        //判断菜单排序号可以为空或传入的不是数值；后台控制有效值在1~99，输入无效时设置数据库中为100
        if (StringUtils.isBlank(dto.getOrder())) {
            en.setOdr(100);
        } else {
            try {
                int order = Integer.parseInt(dto.getOrder());
                if (order < 1 || order > 100) {
                    en.setOdr(100);
                } else {
                    en.setOdr(order);
                }
            } catch (NumberFormatException e) {
                en.setOdr(100); //输入无效值设置数据库中为100
            }
        }

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (!StringUtils.isBlank(dto.getFlag())) {
            try {
                flag = Integer.parseInt(dto.getFlag());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    /**
     * 根据枚举key获取枚举对象
     *
     * @param index
     * @return
     */
    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }
}
